package ddhost.cbs;

// btLog.txtに，日時やBDAを書き込むスレッド

import java.io.File;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;


public class fileWriteThread extends Thread implements Runnable {

	String addr = "null";
	
	public fileWriteThread(String _addr){
		addr = _addr;
	}
	
	@Override
	public void run() {
		
		//日時と改行を付加して書き込む
		addr = getDateTime() + " " + addr + "\n";
		
		byte[] bytes = null;
		try {
			bytes = addr.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		File file = new File("/sdcard/btLog.txt");
		
		// wentbyListがなければ作成
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				errorLogWriter logw = new errorLogWriter("couldn't create wentbyList.txt");
				logw.start();
			}
		}
		
		//書き込み
		try {
			OutputStream output = new FileOutputStream(file,true);
			
			output.write(bytes);
			
			output.flush();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			errorLogWriter logw = new errorLogWriter(e.getMessage());
			logw.start();
		} 
		
		
	}
	
	
	public String getDateTime(){
		GregorianCalendar cal1 = new GregorianCalendar();
		SimpleDateFormat fmt1 = new SimpleDateFormat(
        "yyyy/MM/dd hh:mm:ss");
	      return fmt1.format(cal1.getTime());
	}
}
